const fs = require('fs-promise');
const http = require('http');
const express = require('express');

const db = require('./conn');
const app = express();

const port = 8001;

(async function work(){
	try{
		let dirs = ['pre','route','post'];
		for(let dir of dirs){
			let files = await fs.readdir(dir);
			files = files.sort().filter(o=>o.endsWith('.js'));
			for(let file of files){
				let tmp = require('./' + dir + '/' + file);
				if (dir === 'route') {
					tmp(app);
				}else{
					app.use(tmp);
				}
			}
		}
		http.createServer(app).listen(port);
	}catch(e){
		console.log(e);
	}
})();

